<template>
  <div class="friend-manager-page">
    <div class="friend-manager-main">
      <div class="dialog-header">
        <span>好友管理</span>
      </div>
      <form class="add-friend-form" @submit.prevent="handleAddFriend">
        <input v-model="addFriendName" type="text" placeholder="输入用户名添加好友" maxlength="20" />
        <button type="submit">添加</button>
      </form>
      <div v-if="errorMsg" class="error-msg">{{ errorMsg }}</div>
      <div class="friend-list-section">
        <h4>我的好友</h4>
        <ul class="friend-list">
          <li v-for="friend in friends" :key="friend">
            <span class="friend-name">{{ friend }}</span>
            <button class="delete-btn" @click="handleDeleteFriend(friend)">删除</button>
          </li>
        </ul>
        <div v-if="!friends.length" class="empty-tip">暂无好友</div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
const props = defineProps({
  friends: Array,
  username: String
})
const emit = defineEmits(['friend-added', 'friend-removed'])
const addFriendName = ref('')
const errorMsg = ref('')

const handleAddFriend = async () => {
  errorMsg.value = ''
  const name = addFriendName.value.trim()
  if (!name) {
    errorMsg.value = '请输入好友用户名'
    return
  }
  if (name === props.username) {
    errorMsg.value = '不能添加自己为好友'
    return
  }
  try {
    const res = await fetch('http://localhost:3000/add_friend', {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({ username: props.username, friend: name })
    })
    const data = await res.json()
    if (!data.success) {
      errorMsg.value = data.msg || '添加失败'
      return
    }
    emit('friend-added', name)
    addFriendName.value = ''
  } catch (e) {
    errorMsg.value = '网络错误'
  }
}

const handleDeleteFriend = async (friend) => {
  errorMsg.value = ''
  try {
    const res = await fetch('http://localhost:3000/remove_friend', {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({ username: props.username, friend })
    })
    const data = await res.json()
    if (!data.success) {
      errorMsg.value = data.msg || '删除失败'
      return
    }
    emit('friend-removed', friend)
  } catch (e) {
    errorMsg.value = '网络错误'
  }
}
</script>

<style scoped>
.friend-manager-page {
  width: 100%;
  height: 100%;
  background: linear-gradient(135deg, #e8f5e9 0%, #b2dfdb 100%);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
  padding-top: 60px;
}

.friend-manager-main {
  width: 80vw;
  max-width: 900px;
  min-width: 320px;
  background: #fff;
  border-radius: 18px;
  box-shadow: 0 8px 32px rgba(76, 175, 80, 0.10);
  padding: 48px 48px 36px 48px;
  display: flex;
  flex-direction: column;
  align-items: stretch;
}

.dialog-header {
  font-size: 2.2rem;
  font-weight: 700;
  margin-bottom: 36px;
  text-align: left;
  color: #43a047;
  letter-spacing: 2px;
  display: flex;
  align-items: center;
  gap: 12px;
}

.add-friend-form {
  display: flex;
  gap: 18px;
  margin-bottom: 22px;
}

.add-friend-form input {
  flex: 1;
  padding: 16px 20px;
  border: 1.5px solid #b2dfdb;
  border-radius: 10px;
  font-size: 1.15rem;
  background: #f1f8e9;
  transition: border-color 0.2s;
}
.add-friend-form input:focus {
  border-color: #43a047;
}

.add-friend-form button {
  padding: 14px 36px;
  background: linear-gradient(90deg, #43a047 0%, #66bb6a 100%);
  color: #fff;
  border: none;
  border-radius: 10px;
  cursor: pointer;
  font-size: 1.15rem;
  font-weight: 600;
  box-shadow: 0 2px 8px rgba(76, 175, 80, 0.08);
  transition: background 0.2s;
}
.add-friend-form button:hover {
  background: linear-gradient(90deg, #388e3c 0%, #43a047 100%);
}

.friend-list-section {
  margin-top: 36px;
}

.friend-list-section h4 {
  margin-bottom: 18px;
  font-size: 1.25rem;
  color: #43a047;
  font-weight: 600;
}

.friend-list {
  list-style: none;
  padding: 0;
  margin: 0;
}

.friend-list li {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 0;
  border-bottom: 1px solid #e0f2f1;
  font-size: 1.13rem;
}

.friend-name {
  color: #388e3c;
  font-weight: 500;
}

.delete-btn {
  background: linear-gradient(90deg, #e57373 0%, #f44336 100%);
  color: #fff;
  border: none;
  border-radius: 8px;
  padding: 8px 22px;
  cursor: pointer;
  font-size: 1rem;
  font-weight: 500;
  box-shadow: 0 2px 8px rgba(244, 67, 54, 0.08);
  transition: background 0.2s;
}
.delete-btn:hover {
  background: linear-gradient(90deg, #c62828 0%, #e57373 100%);
}

.error-msg {
  color: #f44336;
  margin-bottom: 14px;
  font-size: 1.08rem;
}

.empty-tip {
  color: #aaa;
  font-size: 1.08rem;
  text-align: center;
  margin-top: 20px;
}

@media (max-width: 600px) {
  .friend-manager-main {
    max-width: 98vw;
    padding: 16px 6px 12px 6px;
  }
  .dialog-header {
    font-size: 1.2rem;
  }
  .add-friend-form input {
    font-size: 1rem;
    padding: 8px 6px;
  }
  .add-friend-form button {
    font-size: 1rem;
    padding: 8px 10px;
  }
}
</style>
